package com.javabasic.algorithm.leetcode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see {EASY} -- [217. Contains Duplicate](https://leetcode-cn.com/problems/contains-duplicate/)
 * @since Created by work on 2021/12/29 11:05 下午
 */
public class ContainsDuplicate {

    /**
     * 排序+检查
     *
     * @param nums
     * @return
     */
    public boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums);
        int len = nums.length;
        for (int i = 1; i < len; i++) {
            if (nums[i] == nums[i - 1]) {
                return true;
            }
        }
        return false;
    }

    /**
     * Set+遍历
     *
     * @param nums
     * @return
     */
    public boolean containsDuplicate2(int[] nums) {
        Set<Integer> numSet = new HashSet<>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            if (numSet.contains(nums[i])) {
                return true;
            }
            numSet.add(nums[i]);
        }
        return false;
    }

}
